package bs.app.android;

import java.io.IOException;

import android.media.MediaPlayer;
import android.media.SoundPool;
import android.media.MediaPlayer.OnCompletionListener;

public class Player {
	public static final int UNKNOWN=-1;
	public static final int STARTED=0;
	public static final int REALIZED = 1;
	public static final int PREFETCHED = 2;
	public static final int UNREALIZED = 3;
	public static final int CLOSED = 4;
	
	public final static int 	SP_MAX_STEAMS 			= 20;
	public final static int 	SP_STEAM_TYPE 			= android.media.AudioManager.STREAM_MUSIC;
//	public final static float	SP_LEFT_VOLUME			= 1.0f;
//	public final static float	SP_RIGHT_VOLUME			= 1.0f;
	public final static float	SP_SOUND_RATE			= 1.0f;
	
	private MediaPlayer mSounds = null;
	private int state = UNKNOWN;
	private static SoundPool mSoundPool = null;
	private int	mSoundPoolIndexLoad = -1;
	
	private boolean isSFX = true;
	private VolumeControl vol;
	private int nLoopCount = 1;
	
	public Player(int resID, boolean isSFX){
		vol = new VolumeControl(this);
		this.isSFX = isSFX;
		if(this.isSFX){
			if(mSoundPool==null){
				mSoundPool = new SoundPool(SP_MAX_STEAMS, SP_STEAM_TYPE, 0);
				mSoundPoolIndexLoad = -1;
			}
			else if(mSoundPoolIndexLoad < 0)
				mSoundPoolIndexLoad = mSoundPool.load(Static.App, resID, 1);
		}
		else {
			mSounds = MediaPlayer.create(Static.App, resID);
			mSounds.setOnCompletionListener(new OnCompletionListener() {
				public void onCompletion(MediaPlayer mp) {
						//state = STATE_PLAYBACK_COMPLETED;
						//System.out.println(">>> onCompletion!");
					}
				}
			);
		}
		state = UNKNOWN;
	}
	
	public static void destroySoundPool() {
//		try{
//			stopAllSounds();
//			if(mSoundPool != null) {
//				releaseSoundPool();
//				mSoundPool.release();
//				mSoundPool = null;
//			}
//			m_bInit = false;
//		}
//		catch(Exception ex)
//		{
//		ex.printStackTrace();
//		}
	}
	
	public int getState() {
		if(state != CLOSED && mSounds.isPlaying())
			return STARTED;
		return state;
	}
	public void prefetch() throws MediaException {
		if(!isSFX){
			try {
				mSounds.prepare();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//if(state == REALIZED)
		state = PREFETCHED;
	}
	public void setLoopCount(int loop) {
		if(loop==0) return;
		if(loop == -1){
			if(isSFX){
				mSoundPool.setLoop(mSoundPoolIndexLoad, -1);
			}
			else{
				if(mSounds != null)	mSounds.setLooping(true);
			}
		}
		else {
			nLoopCount  = loop;
		}
	}
	public void realize() throws MediaException {
		//if(state == UNREALIZED)
		state = REALIZED;
	}
	public void start() throws MediaException {
		if(isSFX){
			float v = getVolume();
			mSoundPool.play(mSoundPoolIndexLoad, v, v, 0, 0, SP_SOUND_RATE);
		}
		else{
//			if(state != PREFETCHED)
//				prefetch();
			updateVolume();
			mSounds.start();
		}
		state = STARTED;
	}
	protected void updateVolume(){
		float v = getVolume();
		if(isSFX){
			mSoundPool.setVolume(mSoundPoolIndexLoad, v, v);
		} else {
			mSounds.setVolume(v, v);
		}
		//System.out.println("Volume-Level = "+v);
	}
	private float getVolume(){
		if(isSFX){
			return (float)(vol.getLevel()/10.0);
		} else {
			return (float)(vol.getLevel()/10.0);//vol.getLevel()/10;
		}
	}
	public VolumeControl getControl(String ctr) {
		if(ctr.equals("VolumeControl"))
			return vol;
		return null;
	}
	public void stop() throws MediaException {
		if(isSFX)
			mSoundPool.stop(mSoundPoolIndexLoad);
		else if (mSounds != null){
			mSounds.pause();
			//if(state == STARTED)
			state = PREFETCHED;
		}
	}
	public void close() throws MediaException {
		if(isSFX) {
			mSoundPool.unload(mSoundPoolIndexLoad);
			mSoundPoolIndexLoad = -1;
		}
		else {
			mSounds.release();
			mSounds = null;
			state = CLOSED;
		}
	}
	public void deallocate() {
		if(!isSFX && mSounds!= null){
			mSounds.seekTo(0);
			//if(state == UNREALIZED || state == PREFETCHED)
			state = REALIZED;
		}
	}
}
